package org.dromara.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.bo.TestDemo3Bo;
import org.dromara.demo.domain.vo.TestDemo3Vo;
import org.dromara.demo.service.impl.TestDemo3ServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 测试关联表Controller
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/demo3")
public class TestDemo3Controller extends BaseService {

    private final TestDemo3ServiceImpl testDemo3Service;

    /**
     * 查询测试关联表列表
     */
    @SaCheckPermission("demo:demo3:list")
    @GetMapping("/list")
    public TableDataInfo<TestDemo3Vo> list(@Validated(QueryGroup.class) TestDemo3Bo bo, PageQuery pageQuery) {
        return testDemo3Service.queryPageList(bo, pageQuery);
    }

  /**
   * 导出测试列表
   */
  @SaCheckPermission("demo:demo3:export")
  @Log(title = "测试关联", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(TestDemo3Bo bo, HttpServletResponse response) {
    List<TestDemo3Vo> list = testDemo3Service.queryList(bo);
    ExcelUtil.exportExcel(list, "测试关联", TestDemo3Vo.class, response);
  }

  /**
   * 获取测试详细信息
   * @param id 主键
   */
  @SaCheckPermission("demo:demo3:query")
  @GetMapping(value = {"/","/{id}"})
  public R<TestDemo3Vo> getInfo( @NotNull(message = "主键不能为空") @PathVariable Long id) {
    return R.ok(testDemo3Service.queryById(id));
  }

  /**
   * 新增测试
   */
  @SaCheckPermission("demo:demo3:add")
  @Log(title = "新增测试", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody TestDemo3Bo bo) {
    return toAjax(testDemo3Service.insertByBo(bo));
  }

  /**
   * 修改测试
   */
  @SaCheckPermission("demo:demo3:edit")
  @Log(title = "测试", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody TestDemo3Bo bo) {
    return toAjax(testDemo3Service.updateByBo(bo));
  }

  /**
   * 删除测试
   *
   * @param ids 主键串
   */
  @SaCheckPermission("demo:demo3:remove")
  @Log(title = "测试主从", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return toAjax(testDemo3Service.deleteWithValidByIds(List.of(ids), true));
  }
}
